import numpy as np
import sklearn.pipeline as pl
import sklearn.preprocessing as sp
import sklearn.linear_model as lm
import matplotlib.pyplot as mp
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
# 读取数据
df = pd.read_csv("demo.csv", encoding='gb18030')
x = df[['年']]
y = df[['人口']]
# 创建模型(管线)
model = pl.make_pipeline(
    sp.PolynomialFeatures(10),  # 多项式特征扩展器
    lm.LinearRegression())  # 线性回归器
# 训练模型
model.fit(x, y)
# 根据输入预测输出2030

pred_y = model.predict(x)
test_x = np.linspace(x.min(), x.max(), 1000).reshape(-1, 1)
pred_test_y = model.predict(test_x)
x1 = int(input("请输入年份（如2050）: "))
y1 = model.predict([[x1]])
print("中国人口预计是：%.2f亿"%(y1/10000))

mp.figure('Polynomial Regression', facecolor='lightgray')
mp.title('Polynomial Regression', fontsize=20)
mp.xlabel('Year', fontsize=14)
mp.ylabel('Population', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
mp.scatter(x, y, c='dodgerblue', alpha=0.75, s=60, label='Sample')
mp.plot(test_x, pred_test_y, c='orangered', label='Regression')
mp.legend()
mp.show()